#[path=r"service/UserService.rs"]
mod UserService;
#[path=r"service/LoginService.rs"]
mod LoginService;
mod my_sql_config;
mod Constant;
#[path="./util/keep_alive.rs"]
mod keep_alive;

use actix_web::{web, App, HttpServer};
use sqlx::mysql::MySqlPoolOptions;
use UserService::*;
use LoginService::*;
use my_sql_config::MySqlConfig;
use Constant::*;
use std::sync::Arc;
use sqlx::MySqlPool;
use keep_alive::*;

#[actix_web::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    unsafe {
        std::env::set_var("RUST_LOG", "actix_web=debug,info"); // 调整日志等级
        env_logger::init();
    }
    let pool = MySqlPoolOptions::new()
        .max_connections(100)
        .connect(&(MySqlConfig::url()))
        .await?;

    let pool_arc = Arc::new(pool.clone());

    HttpServer::new(move || {
        App::new()
            .app_data(web::Data::new(pool.clone()))
            // .wrap(keep_alive())  // 使用 keep_alive 中间件
            .service(
                web::scope("/user")
                    .service(get_users)
                    .service(get_user_by_id)
                    .service(create_user)
                    .service(update_user)
                    .service(delete_user),
            )
            .service(
                web::scope("/auth")
                    .service(login),
            )
    })
    .bind(format!("{}:{}", IP, PORT))?
    .run()
    .await?;

    log::info!("Rust成功启动! 地址为: https://{}:{}", IP, PORT);

    Ok(())
}